﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;

namespace DienLib.PopUp
{
    public class Command
    {
        /// <summary>
        /// Highlight the Input String Match Expression & return InlineCollection
        /// </summary>
        /// <param name="input">Input String for search Match</param>
        /// <param name="matchRegex">Simple Match without () or with (?:) only</param>
        /// <param name="colour">colour for Match</param>
        /// <param name="bold">bold or not Match</param>
        /// <returns>InlineCollection to AddRange to TextBlock</returns>
        public static System.Windows.Documents.Inline[] HighlightTextBlockMatch(string input, string matchRegex, System.Windows.Media.Brush colour, bool bold)
        {   // Need to inplement more about case match "*" but appear single quote " as inch unit. Consider USE : (?! as Regex Invalidate
            string[] split = System.Text.RegularExpressions.Regex.Split(input, "(" + matchRegex + ")");
            List<System.Windows.Documents.Inline> iList = new List<System.Windows.Documents.Inline>();
            foreach (string s in split)
            {
                if (Regex.IsMatch(s, matchRegex))
                {   // s Match Regex
                    System.Windows.Documents.Run run = new System.Windows.Documents.Run(s);
                    if (colour != null) run.Foreground = colour;
                    if (bold) iList.Add(new System.Windows.Documents.Bold(run)); else iList.Add(run);
                }
                else iList.Add(new System.Windows.Documents.Run(s));
            }
            return iList.ToArray();
        }

        /// <summary>
        /// return filePath.
        /// </summary>
        /// <param name="title"></param>
        /// <param name="fileType">eg : "Text files (*.txt)|*.txt|All files (*.*)|*.*"</param>
        /// <returns></returns>
        public static string OpenFileDialog(string title = "Open A File", string fileType = "All files (*.*)|*.*", string startupPath = "")
        {
            System.Windows.Forms.OpenFileDialog fDialog = new System.Windows.Forms.OpenFileDialog();
            fDialog.Title = title;
            fDialog.Filter = fileType; //"DemoFarpac EXCEL File|*.xlsx";
            fDialog.InitialDirectory = startupPath;
            if (fDialog.ShowDialog() == System.Windows.Forms.DialogResult.OK)
            {
                //System.Windows.Forms.MessageBox.Show(fDialog.FileName.ToString());
                return fDialog.FileName;
            }
            else return null;
        }
    }
}
